package com.allwees.bs.module.oms.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.allwees.bs.module.oms.entity.OmsOrderItem;
import com.allwees.core.common.constant.EStatus;
import com.allwees.core.dao.annotation.NoDataPermission;
import com.allwees.core.dao.mybatis.dao.BaseDao;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

public interface OmsOrderItemDao extends BaseDao<OmsOrderItem> {

    @Update({"UPDATE oms_order_item set settle_state = null WHERE uuid = #{omsOrderItemUuid}"})
    void clearSettleStateByUuid(String omsOrderItemUuid);

    Set<String> findOrderUuids(@Param("uuids") Set<String> uuids);

    List<String> findUuidsByOrderUuids(@Param("orderUuids") Set<String> orderUuids);

    default Integer queryCountByOrderUuid(String orderUuid) {
        return queryCountByOrderUuidAndStatus(orderUuid, null);
    }

    @NoDataPermission
    default Integer queryCountByOrderUuidAndStatus(String orderUuid, EStatus status) {
        QueryWrapper<OmsOrderItem> wrapper = new QueryWrapper<>();
        wrapper.eq("order_uuid", orderUuid);
        if (status != null) {
            wrapper.eq("status", status.value());
        }
        return selectCount(wrapper);
    }

    default List<OmsOrderItem> findByOrderUuid(String orderUuid) {
        return findListByField("order_uuid", orderUuid);
    }

    @Select({"<script>",
            "SELECT",
            "  COUNT(ID)",
            " FROM oms_order_item o",
            " WHERE review_at is null AND refunded = false AND canceled = false AND order_uuid = #{orderUuid}",
            "</script>"
    })
    @NoDataPermission
    Integer findUnReviewItemCount(String orderUuid);

    @Select({"<script>",
            "SELECT",
            "  o.uuid",
            " FROM oms_order_item o",
            " WHERE status = 38",
            "</script>"
    })
    List<String> findShipped();

    @Select({"<script>",
            "SELECT",
            "  * ",
            " FROM oms_order_item o",
            " WHERE status = 30",
            "</script>"
    })
    List<OmsOrderItem> findUnShipped();

    @Select({"<script>",
            "SELECT",
            "  * ",
            " FROM oms_order_item o",
            " WHERE status = #{status}",
            " AND payment_at  &lt;  from_unixtime(unix_timestamp() - #{overTimeUnShipped}) ", //查询超过7天未发货的订单
            "</script>"
    })
    List<OmsOrderItem> findUnShippedUuids(Integer status, Integer overTimeUnShipped);

    @Select({"<script>",
            "SELECT * ",
            " FROM oms_order_item o",
            " WHERE  deliver_at is not null  and  deliver_at > from_unixtime(unix_timestamp() - 3600*24*60)  and settle_state = #{status}   ", //仅查询妥投后60天内的订单
            "</script>"
    })
    List<OmsOrderItem> findCanSettleds(@Param("status") Integer status);

    @Select({"<script>",
            "SELECT * ",
            " FROM oms_order_item o",
            " WHERE settle_state = #{status}  and uuid = #{omsOrderItemUuid}  and (retailer_uuid = #{branchUuid} or supplier_uuid = #{branchUuid}) ",
            "</script>"
    })
   OmsOrderItem findSettleItems( @Param("status") Integer status,  @Param("omsOrderItemUuid") String omsOrderItemUuid , @Param("branchUuid") String branchUuid );

    @Select({"<script>",
            "SELECT SUM(o.platform_amt_sku)",
            " FROM oms_order_item o",
            " WHERE refunded = true and refund_at > #{currentDay}",
            "</script>"
    })
    BigDecimal findTodayRefundDealAmt(@Param("currentDay") Date currentDay);

    @Select({"<script>",
            "SELECT SUM(platform_amt)",
            " FROM o_order o",
            " WHERE payment_at is not null ",
            "</script>"
    })
    BigDecimal findTotalDealAmt();

    @Select({"<script>",
            "SELECT SUM(o.platform_amt_sku)",
            " FROM oms_order_item o",
            " WHERE is_deleted = 0 AND payment_at > #{currentDay}",
            "</script>"
    })
    BigDecimal findTodayDealAmt(@Param("currentDay") Date currentDay);

    @Select({"<script>",
            "SELECT SUM(o.platform_amt_sku)",
            " FROM oms_order_item o",
            " WHERE is_deleted = 0 AND payment_at > #{yesterday} AND payment_at &lt; #{current}",
            "</script>"
    })
    BigDecimal findYesterdayDealAmt(@Param("yesterday") Date yesterday, @Param("current") Date current);

    default Integer queryCountByStatus(EStatus status) {
        QueryWrapper<OmsOrderItem> wrapper = new QueryWrapper<>();
        wrapper.eq("status", status.value());
        return selectCount(wrapper);
    }

    default List<OmsOrderItem> findByOrderUuids(Set<String> orderUuids){
        if (CollectionUtils.isEmpty(orderUuids)) {
            return new ArrayList<>(0);
        }
        QueryWrapper<OmsOrderItem> wrapper = new QueryWrapper<>();
        wrapper.in("order_uuid", orderUuids);
        return selectList(wrapper);
    }

    default List<OmsOrderItem> selectListByOrderUuid(String orderUuid) {
        if (StringUtils.isEmpty(orderUuid)) {
            return new ArrayList<>(0);
        }
        QueryWrapper<OmsOrderItem> wrapper = new QueryWrapper<>();
        wrapper.eq("order_uuid", orderUuid);
        return selectList(wrapper);
    }
}
